package com.chengh.nami.web.controller.system;

import com.chengh.nami.common.domain.pojo.SysDictData;
import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.request.PageQuery;
import com.chengh.nami.common.response.PageData;
import com.chengh.nami.common.response.R;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.service.ISysDictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author chengh
 * @Date 2023-01-02 14:51
 * @Description: 字典数据控制器
 */
@Tag(name = "SysDictDataController", description = "字典数据管理")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/dict/data")
public class SysDictDataController {

    private final ISysDictDataService dictDataService;

    @Operation(summary = "根据字典类型获取字典数据")
    @GetMapping("/{dictType}")
    public R<List<SysDictData>> data(@PathVariable String dictType) {
        List<SysDictData> dictDataByType = dictDataService.getDictDataByType(dictType);
        return R.ok(dictDataByType);
    }

    /**
     * 分页获取字典数据列表
     *
     * @param dictData  查询条件
     * @param pageQuery 分页信息
     * @return 字典数据列表
     */
    @GetMapping()
    @Operation(summary = "获取字典数据列表信息", description = "")
    public R<PageData<SysDictData>> list(SysDictData dictData, PageQuery pageQuery) {
        PageData<SysDictData> pageData = dictDataService.getList(dictData, pageQuery);
        return R.ok(pageData);
    }

    /**
     * 根据ID获取字典数据信息
     *
     * @param id 用户ID
     * @return 信息
     */
    @GetMapping("/getData/{id}")
    @Operation(summary = "根据字典数据ID获取信息", description = "")
    public R<SysDictData> data(@PathVariable Long id) {
        return R.ok(dictDataService.getById(id));
    }

    /**
     * 根据ID修改字典数据信息
     *
     * @param dictData 字典信息
     * @return 是否成功
     */
    @PutMapping()
    @Operation(summary = "根据ID修改字典数据信息", description = "")
    public R<Void> edit(@RequestBody SysDictData dictData) {
        verifyDictDate(dictData);
        return R.bool(dictDataService.edit(dictData));
    }

    /**
     * 新增字典数据
     *
     * @param dictData 字典信息
     * @return 是否成功
     */
    @PostMapping()
    @Operation(summary = "新增字典数据", description = "")
    R<Void> add(@RequestBody SysDictData dictData) {
        verifyDictDate(dictData);
        return R.bool(dictDataService.aaddData(dictData));
    }

    /**
     * 根据ID集合删除字典数据信息
     *
     * @param ids id集合
     * @return 是否成功
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "根据ID集合删除字典数据信息", description = "")
    public R<Void> delDatas(@PathVariable Long[] ids) {
        return R.bool(dictDataService.delInfoByIds(Arrays.asList(ids)));
    }

    private void verifyDictDate(SysDictData dictData) {
        if (StringUtils.isEmpty(dictData.getDictType())) {
            throw new BizException("字典类型不能为空");
        }
        if (StringUtils.isEmpty(dictData.getDictLabel())) {
            throw new BizException("字典标签不能为空");
        }
        if (StringUtils.isEmpty(dictData.getDictValue())) {
            throw new BizException("字典值不能为空");
        }
    }
}
